/*
 moxl - Modular XML Library
 Copyright (c) 2007, Malcolm Sparks

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package moxl.xpath;

import moxl.dom.MoxlNode;
import moxl.xpath.parser.states.step.Function;

/**
 * From the XPath spec: "A context consists of a node, a pair of non-zero positive integers (the context position and the context size), a set of variable
 * bindings, a function library, the set of namespace declarations in scope for the expression".
 */
public class XPathContext
{
	private final MoxlNode node;
	private final FunctionMap functions;
	private final int contextPosition, contextSize;

	public XPathContext(MoxlNode node, FunctionMap functions)
	{
		this(node, functions, 1, 1);
	}

	private XPathContext(MoxlNode node, FunctionMap functions, int contextPosition, int contextSize)
	{
		if (contextPosition < 1)
		{
			throw new IllegalArgumentException("Context position cannot be less than 1");
		}
		if (contextSize < 1)
		{
			throw new IllegalArgumentException("Context size cannot be less than 1");
		}
		this.node = node;
		this.functions = functions;
		this.contextPosition = contextPosition;
		this.contextSize = contextSize;
	}

	public MoxlNode getContextNode()
	{
		return node;
	}

	public XPathContext beget(MoxlNode contextNode, int contextPosition, int contextSize)
	{
		return new XPathContext(contextNode, functions, contextPosition, contextSize);
	}

	public Function getFunction(String name)
	{
		return functions.getFunction(name);
	}

	public int getPosition()
	{
		return contextPosition;
	}

	public int getSize()
	{
		return contextSize;
	}
}
